/*
 * @Author: ygnnli 1585923072@qq.com
 * @Date: 2024-08-24 18:30:02
 * @LastEditors: ygnnli 1585923072@qq.com
 * @LastEditTime: 2024-08-26 21:52:16
 * @FilePath: /boost_searcher/http_server.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE#include "cpp-htt"
 */
#include "searcher.hpp"
#include "cpp-httplib/httplib.h"


const std::string root_path = "./wwwroot";
const std::string input = "data/raw_html/raw.txt";

int main()
{
    ns_searcher::Searcher search;
    search.InitSearcher(input); // 初始化搜索器


    httplib::Server svr;

    svr.set_base_dir(root_path.c_str());//设置静态资源目录
    //捕捉search请求
    svr.Get("/s", [&search](const httplib::Request &req, httplib::Response &rsp)
            { if(!req.has_param("word"))//判断是否含有搜索关键字
            {
                rsp.set_content("必须要有搜索关键字!","text/plain; charset=utf-8");//设置响应内容 text/plain:是纯文本  --- 对照表类型Content-Type
                return;
            }
            std::string word = req.get_param_value("word");//获取用户输入的搜索关键字
            //std::cout<<"用户输入的搜索关键字为:"<<word<<std::endl;
            LOG(NORMAL, "用户输入的搜索关键字为:"+word);
            std::string json_string;
            search.Search(word,&json_string);//调用搜索器进行搜索
            rsp.set_content(json_string,"application/json");//设置响应内容的 application/json:是json类型Content-Type
            ; });
    
    //服务器启动:监听
    svr.listen("0.0.0.0", 8081);//"0.0.0.0"表示监听所有ip地址 8081表示监听的端口号

    return 0;
}